package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.entity.ImgReq;
import com.easy.dfcapi.res.*;
import com.easy.dfcapi.service.ApiUploadService;
import com.easy.dfcapi.service.ApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/user")
public class ApiUserController extends Api20Controller {


    @Autowired
    private ApiUserService userService;

    @Autowired
    private ApiUploadService imageService;

    /**
     * 验证码
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "GetVerSmsCode", method = RequestMethod.POST)
    public Result GetVerSmsCode(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return userService.GetVerSmsCode(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 密码登录
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UserLogin", method = RequestMethod.POST)
    public Result UserLogin(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return userService.UserLogin(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 验证码登录
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "SmsLogin", method = RequestMethod.POST)
    public Result SmsLogin(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return userService.SmsLogin(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 用户注册
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UserRegister", method = RequestMethod.POST)
    public Result UserRegister(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return userService.UserRegister(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 修改密码
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ModifyPassword", method = RequestMethod.POST)
    public Result ModifyPassword(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.ModifyPassword(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 重置密码
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ResetPassword", method = RequestMethod.POST)
    public Result ResetPassword(ResponseUser response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.ResetPassword(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 用户信息
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UserInfo", method = RequestMethod.POST)
    public Result UserInfo(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.UserInfo(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


    /**
     * 修改呢称
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpdateNick", method = RequestMethod.POST)
    public Result UpdateNick(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.UpdateNick(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 修改手机号
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpdateMobile", method = RequestMethod.POST)
    public Result UpdateMobile(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.UpdateMobile(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 修改登录名称
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpdateLoginName", method = RequestMethod.POST)
    public Result UpdateLoginName(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.UpdateLoginName(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 修改头像
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpdateAvatar", method = RequestMethod.POST)
    public Result UpdateAvatar(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            List<ImgReq> imgList = imageService.UploadImg(request);

            return userService.UpdateAvatar(imgList, response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 设置店铺获取Token
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "RefreshToken", method = RequestMethod.POST)
    public Result RefreshToken(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.RefreshToken(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 检查登录名是否被其他用户使用
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CheckLoginName", method = RequestMethod.POST)
    public Result CheckLoginName(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.CheckLoginName(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 检查手机号是否被其他用户使用
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CheckMobile", method = RequestMethod.POST)
    public Result CheckMobile(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.CheckMobile(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 用户收货地址列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AddressList", method = RequestMethod.POST)
    public Result AddressList(Response<AddressRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.AddressList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 增加用户收货地址
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AddressAdd", method = RequestMethod.POST)
    public Result AddressAdd(Response<AddressRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.AddressAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 用户收货地址修改
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AddressMod", method = RequestMethod.POST)
    public Result AddressMod(Response<AddressRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.AddressMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 用户收货地址删除
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AddressDel", method = RequestMethod.POST)
    public Result AddressDel(Response<AddressRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.AddressDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取用户好友组
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendGroupList", method = RequestMethod.POST)
    public Result FriendGroupList(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendGroupList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取用户好友
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendList", method = RequestMethod.POST)
    public Result FriendList(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 获取用户黑名单名列
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BlackList", method = RequestMethod.POST)
    public Result BlackList(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.BlackList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 新增用户好友
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendAdd", method = RequestMethod.POST)
    public Result FriendAdd(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 新增用户好友
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendDel", method = RequestMethod.POST)
    public Result FriendDel(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 新增用户好友组
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendGroupAdd", method = RequestMethod.POST)
    public Result FriendGroupAdd(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendGroupAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 修改用户好友组名称
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendGroupMod", method = RequestMethod.POST)
    public Result FriendGroupMod(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendGroupMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 删除用户好友组
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendGroupDel", method = RequestMethod.POST)
    public Result FriendGroupDel(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendGroupDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 设置用户好友到好友组
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Friend2Group", method = RequestMethod.POST)
    public Result Friend2Group(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.Friend2Group(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 设置用户好友在好友组备注
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "FriendName", method = RequestMethod.POST)
    public Result FriendName(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.FriendName(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 添加黑名单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BlackAdd", method = RequestMethod.POST)
    public Result BlackAdd(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.BlackAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 删除黑名单
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BlackDel", method = RequestMethod.POST)
    public Result BlackDel(Response<UserGroupRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }

            response.setIp(IPUtil.getIpAddress(request));
            return userService.BlackDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 获取国家列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCountry", method = RequestMethod.GET)
    public Result getCountry(HttpServletRequest request) {
        try {
            return userService.getCountry();
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 获取省份列表
     * @param countryId
     * @param countryName
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getProvince", method = RequestMethod.GET)
    public Result getProvince(String countryId, String countryName, HttpServletRequest request) {
        try {
            return userService.getProvince(countryId, countryName);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 获取城市列表
     * @param provinceId
     * @param provinceName
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCity", method = RequestMethod.GET)
    public Result getCity(String provinceId,String provinceName,HttpServletRequest request) {
        try {
            return userService.getCity(provinceId, provinceName);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 获取国家列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDistrict", method = RequestMethod.GET)
    public Result getDistrict(String cityId, String cityName, HttpServletRequest request) {
        try {
            return userService.getDistrict(cityId, cityName);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

}
